package com.tjbank.cssys.web.controller;


import com.github.pagehelper.PageInfo;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.domain.CkOrderAchievementForm;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.service.CkOrderAchievementFormService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/achievement")
@RestController
@Api(tags = "CkOrderLoanIntelligenceDivisionController", description = "催收员查询")
public class CkOrderAchievementFormController {
    @Autowired
    private CkOrderAchievementFormService ckOrderAchievementFormService;
    @Autowired
    private CkLogService ckLogService;
    private static Logger logger = LoggerFactory.getLogger(CkOrderAchievementFormController.class);
    /**
     * @param collectorId
     * @param collectorName
     */
    @ApiOperation("获取催收员查询管理")
    @RequestMapping("/select")
    public CommonResult selectAll(@RequestParam(required = false, value = "collectorId") String collectorId,
                                  @RequestParam(required = false, value = "collectorName") String collectorName,
                                  @RequestParam("dataTime") String dataTime, HttpServletRequest request) {
        if (StringUtils.isEmpty(collectorId)) {
            collectorId=null;
        }
        if (StringUtils.isEmpty(collectorName)) {
            collectorName=null;
        }
        List<CkOrderAchievementForm> ckOrderAchievementForms = ckOrderAchievementFormService.select(collectorId, collectorName, dataTime);


        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("催收员查询异常",e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setParams(collectorId);
        ckLog.setParams(collectorName);
        ckLog.setParams(dataTime);
        ckLog.setOperationType("获取催收员查询管理");

        if (CollectionUtils.isNotEmpty(ckOrderAchievementForms)) {
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(ckOrderAchievementForms);
        } else {
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(null,"当前时间数据为空");
        }

    }

    /**
     * @param params
     */
    @ApiOperation("获取催收员查询管理")
    @RequestMapping("/list")
    public CommonResult selectAll(@RequestBody Map params ,HttpServletRequest request) {

        PageInfo data = ckOrderAchievementFormService.select(params);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("催收员查询异常",e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setOperationType("获取催收员查询管理");

        return CommonResult.success(data, "查询成功！");
    }
}
